package com.bxd.baodan.widget;

import com.google.gson.Gson;
import com.bxd.baodan.R;
import com.bxd.baodan.app.Constant;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.app.SharedPreferanceKey;
import com.bxd.baodan.item.UserItem;
import com.bxd.baodan.util.DialogUtil;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.util.NetUtil.RequestStringListener;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.preference.PreferenceActivity;
import android.text.format.Time;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;

@SuppressLint("NewApi")
public class DatePickerPreference extends DialogPreference {

	public DatePicker mPicker;
	public long mValue;

	public TextView textValue;

	public DatePickerPreference(Context context, AttributeSet attrs) {
		super(context, attrs);

		setDialogLayoutResource(R.layout.preference_datepicker);

		setNegativeButtonText(null);
		super.setLayoutResource(R.layout.preference_child);
		super.setWidgetLayoutResource(R.layout.preference_child_value);

		setDialogIcon(null);
	}

	@Override
	protected View onCreateView(ViewGroup parent) {
		View view = super.onCreateView(parent);

		textValue = (TextView) view.findViewById(R.id.value);

		if (LoginInfo.getInstance(DatePickerPreference.this.getContext())
				.getUser().getBirthday() != null
				&& !LoginInfo
						.getInstance(DatePickerPreference.this.getContext())
						.getUser().getBirthday().equals("")) {
			textValue.setText(LoginInfo.getInstance(getContext()).getUser()
					.getBirthday());
		} else {
			Time time = new Time();
			time.set(getPersistedLong(mValue));

			textValue.setText(time.format("%Y-%m-%d"));
		}

		return view;
	}

	@Override
	protected Object onGetDefaultValue(TypedArray a, int index) {
		return a.getInteger(index, DEFAULT_ORDER);
	}

	@Override
	protected void onBindDialogView(View view) {
		super.onBindDialogView(view);

		mPicker = (DatePicker) view.findViewById(R.id.datePicker_preference);
		if (mPicker != null) {
			long value = getPersistedLong(mValue);

			Time time = new Time();
			time.setToNow();

			mPicker.setMaxDate(time.toMillis(false));

			time.set(value);
			mPicker.init(time.year, time.month, time.monthDay,
					new OnDateChangedListener() {

						@Override
						public void onDateChanged(DatePicker view, int year,
								int monthOfYear, int dayOfMonth) {
						}

					});

		}

	}

	private void doRequest(final String time, final long value) {

		NetUtil netUtil = new NetUtil((PreferenceActivity) this.getContext(),
				JsonApi.MODIFYINFOR);
		// netUtil.setParams("ver", JsonApi.VAR);
		netUtil.setParams("birthday", time);
		netUtil.postRequest("", new RequestStringListener() {

			@Override
			public void onComplete(String result) {
				if (JSONUtil.isSuccess(result)) {
					DatePickerPreference.this.persistLong(value);
					UserItem item = LoginInfo.getInstance(
							DatePickerPreference.this.getContext().getApplicationContext())
							.getUser();
					item.setBirthday(time);
					LoginInfo.getInstance(DatePickerPreference.this.getContext())
							.saveUserInfo(new Gson().toJson(item),
									DatePickerPreference.this.getContext());
					
					DatePickerPreference.this.getContext().sendBroadcast(
							new Intent(Constant.ACTION_SUCCESS_UPLOAD_INFOR));
				} else {
					DialogUtil.getInstance().showToast(getContext(),
							JSONUtil.getMessage(result));
				}
			}
		});

	}

	@Override
	protected void onDialogClosed(boolean positiveResult) {
		super.onDialogClosed(positiveResult);
		if (positiveResult) {
			Time time = new Time();
			time.set(0, time.second, time.hour, mPicker.getDayOfMonth(),
					mPicker.getMonth(), mPicker.getYear());
			long value = time.toMillis(false);
			if (callChangeListener(value)) {
				if (SharedPreferanceKey.BIRTHDAY
						.equals(DatePickerPreference.this.getKey())) {
					doRequest(time.format("%Y-%m-%d"), value);
				} else {
					super.persistLong(value);
					textValue.setText(time.format("%Y-%m-%d"));
				}
			}
		}
	}

}
